package com.boco.modules.fdoc.web.servicepack;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.persistence.Page;
import com.boco.common.utils.JsonUtils;
import com.boco.modules.fdoc.model.ServicepackDetailEntity;
import com.boco.modules.fdoc.model.SigServicepackEntity;
import com.boco.modules.fdoc.model.SigSvsPackDtlRlnEntity;
import com.boco.modules.fdoc.model.SysRegionEntity;
import com.boco.modules.fdoc.service.ServicePackDetailsService;
import com.boco.modules.fdoc.service.SigServicepackService;
import com.boco.modules.fdoc.service.SigSvsPackDtlRlnService;
import com.boco.modules.fdoc.service.SysRegionService;
import com.boco.modules.fdoc.vo.ServicepackDetailsVo;
import com.boco.modules.fdoc.vo.ServicepackVo;

/**
 * 服务包管理
 * @author lzz
 *
 */
@Controller
@RequestMapping(value="/servicePack",produces = "application/json;charset=UTF-8")
public class ServicePack {

	@Resource
	SysRegionService sRegionService;
	@Resource
	SigServicepackService packService;
	@Resource
	ServicePackDetailsService detailService;
	@Resource
	SigSvsPackDtlRlnService rlnService;
	
	/**
	 * 跳转到服务包主页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/showPack",method = RequestMethod.GET)
	public String showPack(HttpServletRequest request, Model model){
		String regionCode="";
		List<SysRegionEntity> list=sRegionService.getChildrenRegions(regionCode);
		model.addAttribute("oneList", list);
		return "/servicepack/servicePack";
		
	}
	
	/**
	 * 跳转新增服务包页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/addPack",method = RequestMethod.GET)
	public String addPack(HttpServletRequest request, Model model){
		return "/servicepack/addPack";
		
	}
	
	/**
	 * 获取最上级的区划
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getOneLevel",method = RequestMethod.GET)
	public String getOneLevel(HttpServletRequest request, Model model){
		//获取最上级的区划
		String regionCode="";
		List<SysRegionEntity> list=sRegionService.getChildrenRegions(regionCode);
		return JsonUtils.getJsonFormat(list);
		
	}
	
	/**
	 * 获取当前区划子级区划
	 * @param request
	 * @param model
	 * @param regionCode 当前区划码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getChild",method = RequestMethod.GET)
	public String getChild(HttpServletRequest request, Model model,String regionCode){
		//获取子的区划
		List<SysRegionEntity> list=sRegionService.getChildrenRegions(regionCode);
		return JsonUtils.getJsonFormat(list);
		
	}
	
	/**
	 * 获取服务包列表
	 * @param request
	 * @param model
	 * @param pageSize 页码
	 * @param pageIndex 页面大小
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getPackList",method = RequestMethod.GET)
	public String getPackList(HttpServletRequest request, Model model,Integer pageSize, Integer pageIndex){
		//获取包列表
		Page<ServicepackVo> page = new Page<ServicepackVo>(pageIndex,pageSize);
		ServicepackVo servicepackVo=new ServicepackVo();
		servicepackVo.setPage(page);
		List<ServicepackVo> list =packService.getAllbyVo(servicepackVo);
		//System.out.println(JsonUtils.getJsonFormat(list));
		Integer count =packService.getCount();
		return JsonUtils.getJsonFormat(BaseJsonVo.pageList(list,count));
		
	}
	
	/**
	 * 获取所有服务包，不分页
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getPackAll",method = RequestMethod.GET)
	public String getPackAll(HttpServletRequest request, Model model){
		//获取包列表
		List<SigServicepackEntity> list =packService.getAll();
		return JsonUtils.getJsonFormat(BaseJsonVo.success(list));
		
	}
	
	/**
	 * 保存服务包
	 * @param request
	 * @param model
	 * @param updateMan 修改人
	 * @param packName 服务包名
	 * @param adviceGroup 建议人群
	 * @param packPrice 服务包价格
	 * @param packType 服务包种类
	 * @param remarks 备注
	 * @param target 目标
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/savePack",method = RequestMethod.POST)
	public String savePack(HttpServletRequest request,Model model,String updateMan,String packName,
			String adviceGroup,Double packPrice,String packType,String remarks,String target){
		SigServicepackEntity sEntity=new SigServicepackEntity();
		List<SigServicepackEntity> list=packService.getAll();
		int i=0;
		for(SigServicepackEntity s:list){
			if(s!=null && !s.equals("")){
				//判断是否已有相同服务包名
				if(s.getPackName().equals(packName)){
					return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.REPEAT_ESERVICEPACK.getKey(), ApiStatusEnum.REPEAT_ESERVICEPACK.getValue()));
				}
				i++;
			}
		}
		Integer pValue=packService.getMaxValue();
		Integer packValue=1;
		if(pValue!=null){
			packValue=2*(pValue);
		}
		
		//Integer packValue=Integer.valueOf((int) Math.pow(2, i));
		sEntity.setPackValue(packValue);
		sEntity.setPackName(packName);
		sEntity.setPackPrice(packPrice);
		sEntity.setUserPay(packPrice);
		sEntity.setAdviceGroup(adviceGroup);
		sEntity.setCreateby(updateMan);
		sEntity.setUpdateby(updateMan);
		Date date=new Date();
		sEntity.setCreatetime(date);
		sEntity.setUpdatetime(date);
		sEntity.setRemarks(remarks);
		sEntity.setPackType(packType);
		sEntity.setDelFlag((short)0);
		sEntity.setTarget(target);
		
		int flag=packService.addPack(sEntity);
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
	
	/**
	 * 删除服务包
	 * @param request
	 * @param model
	 * @param id 服务包id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deletePack",method = RequestMethod.POST)
	public String deletePack(HttpServletRequest request,Model model,Integer id){
		int flag=packService.deletePack(id);
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}
	
	/**
	 * 修改服务包
	 * @param request
	 * @param model
	 * @param id 服务包id
	 * @param updateMan 修改人
	 * @param packName 服务包名
	 * @param adviceGroup 建议人群
	 * @param packPrice 价格
	 * @param userPay 用户需支付
	 * @param packType 服务包种类
	 * @param remarks 备注
	 * @param target 目标
	 * @param orgratio 机构分成占比
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updatePack",method = RequestMethod.POST)
	public String updatePack(HttpServletRequest request,Model model,Integer id,String updateMan,String packName,
			String adviceGroup,Double packPrice,Double userPay,String packType,String remarks,String target,Float orgratio){
		
		SigServicepackEntity sEntity=new SigServicepackEntity();
		
		sEntity.setId(id);
		sEntity.setPackName(packName);
		sEntity.setPackPrice(packPrice);
		sEntity.setUserPay(userPay);
		sEntity.setAdviceGroup(adviceGroup);
		sEntity.setUpdateby(updateMan);
		Date date=new Date();
		sEntity.setUpdatetime(date);
		sEntity.setRemarks(remarks);
		sEntity.setPackType(packType);
		sEntity.setTarget(target);
		sEntity.setOrgratio(orgratio);
		
		int flag=packService.updatePack(sEntity);
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
	
	/**
	 * 根据服务包id获取服务包详细信息
	 * @param request
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getPackbyId",method = RequestMethod.GET)
	public String getPackbyId(HttpServletRequest request,Model model,Integer id){
		SigServicepackEntity sEntity=new SigServicepackEntity();
		sEntity=packService.getPackbyId(id);
		model.addAttribute("sEntity", sEntity);
		return "/servicepack/updatePack";
	}
	
	/**
	 * 跳转服务项目详情页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/showDetail",method = RequestMethod.GET)
	public String showDetail(HttpServletRequest request,Model model){
		List<SigServicepackEntity> packList =packService.getAll();
		String regionCode="";
		List<SysRegionEntity> list=sRegionService.getChildrenRegions(regionCode);
		model.addAttribute("packList", packList);
		model.addAttribute("oneList", list);
		return "/servicepack/servicePackDetail";
	}
	
	/**
	 * 展示服务项目详情（新）
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/showDetailNew",method = RequestMethod.GET)
	public String showDetailNew(HttpServletRequest request,Model model){
		List<SigServicepackEntity> packList =packService.getAll();
		String regionCode="";
		List<SysRegionEntity> list=sRegionService.getChildrenRegions(regionCode);
		model.addAttribute("packList", packList);
		model.addAttribute("oneList", list);
		return "/servicepack/servicePackDetailNew";
	}
	
	/**
	 * 根据服务包id 获取其下服务项目列表
	 * @param request
	 * @param model
	 * @param id 服务包id
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getPackDetails",method=RequestMethod.GET)
	public String getPackDetails(HttpServletRequest request,Model model,String id,Integer pageSize,Integer pageIndex){
		//SysRegionEntity regionEntity=sRegionService.getRegionByCode(regionCode);
		//String regionId=regionEntity.getId();	
		
		Map<String, Object> map=new HashMap<String, Object>();
		if(id.equals("-1")){
			id="";
		}
		map.put("id", id);
		map.put("regionId", "");
		
		
		List<ServicepackDetailEntity> detailList =packService.getDetailList(map);
		Integer count=detailList.size();
		return JsonUtils.getJson(BaseJsonVo.pageList(detailList, count));
	}
	
	/**
	 * 根据服务包名称获取其下服务项目
	 * @param request
	 * @param model
	 * @param serviceName
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getPackDetailsByName",method=RequestMethod.GET)
	public String getPackDetailsByName(HttpServletRequest request,Model model,String serviceName,Integer pageSize,Integer pageIndex){
		//SysRegionEntity regionEntity=sRegionService.getRegionByCode(regionCode);
		//String regionId=regionEntity.getId();	
		
		ServicepackDetailsVo sVo=new ServicepackDetailsVo();
		Page<ServicepackDetailsVo> page=new Page<ServicepackDetailsVo>(pageIndex, pageSize);
		sVo.setPage(page);
		sVo.setServiceName(serviceName);
		List<ServicepackDetailEntity> detailList =detailService.getDetailListByName(sVo);
		Integer count=detailService.getCount();
		return JsonUtils.getJson(BaseJsonVo.pageList(detailList,count));
	}
	
	
	@RequestMapping(value="/updatePackDetail",method=RequestMethod.GET)
	public String updatePackDetail(HttpServletRequest request,Model model,String id){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("id", id);
		map.put("regionId", "");
		List<ServicepackDetailEntity> detailList =packService.getDetailList(map);
		List<SigServicepackEntity> packList =packService.getAll();
		model.addAttribute("id", id);
		model.addAttribute("packList", packList);
		model.addAttribute("detailList", JsonUtils.getJson(detailList));
		return "/servicepack/updateDetail";
	}
	@Transactional
	@ResponseBody
	@RequestMapping(value="/savePackDetail",method=RequestMethod.POST)
	public String savePackDetail(HttpServletRequest request,Model model,String id,String sden){
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("id", id);
		map.put("regionId", "");
		Date date=new Date();
		ServicepackDetailEntity sdEntity=new ServicepackDetailEntity();
		List<ServicepackDetailEntity> detailList =packService.getDetailList(map);
		List<Map> returnList=JsonUtils.getObjectList(sden, Map.class);
		int i=1,j=1,p=1,k=1;
		for(ServicepackDetailEntity sEntity:detailList){
			int flag=0;
			for(Map m1:returnList){
				if(sEntity.getId().equals(Integer.valueOf(m1.get("id").toString()))){
						sdEntity.setId(Integer.valueOf((String) m1.get("id")));
						sdEntity.setServiceName((String)m1.get("serviceName"));
						//sdEntity.setServiceItem((String)m1.get("serviceItem"));
						sdEntity.setServiceDetails((String)m1.get("serviceDetails"));
						sdEntity.setFrequency(Integer.valueOf((String) m1.get("frequency")));
						//sdEntity.setOriginalPrice(Double.parseDouble((String)m1.get("originalPrice")));
						sdEntity.setServiceType((String)m1.get("serviceType"));
						sdEntity.setUpdateby((String)m1.get("updateMan"));
						sdEntity.setUpdatetime(date);
						flag=1;
						
						//修改方法
						j=detailService.updatePackDetail(sdEntity);
						break;
				}
			}
			if(flag==0){
				SigSvsPackDtlRlnEntity rlnEntity=new SigSvsPackDtlRlnEntity();
				Integer detailsId=sEntity.getId();
				rlnEntity.setSigId(Integer.valueOf(id));
				rlnEntity.setDetailsId(detailsId);
				//删除方法,将关联表中的相关关系逻辑去除
				k=rlnService.updateRln(rlnEntity);
			}
			
		}
		
		for(Map m1:returnList){
			System.out.println(sdEntity.toString());
			if(com.boco.common.utils.StringUtils.isEmpty((String)m1.get("id"))){
				    ServicepackDetailEntity spdEntity=new ServicepackDetailEntity();
					spdEntity.setServiceName((String)m1.get("serviceName"));
					//spdEntity.setServiceItem((String)m1.get("serviceItem"));
					spdEntity.setServiceDetails((String)m1.get("serviceDetails"));
					spdEntity.setFrequency(Integer.valueOf((String) m1.get("frequency")));
					//spdEntity.setOriginalPrice(Double.parseDouble((String)m1.get("originalPrice")));
					spdEntity.setServiceType((String)m1.get("serviceType"));
					spdEntity.setUpdateby((String)m1.get("updateMan"));
					spdEntity.setUpdatetime(date);
					spdEntity.setCreateby((String)m1.get("updateMan"));
					spdEntity.setCreatetime(date);
					spdEntity.setDelFlag(0);
					//新增项目
					
					p=detailService.addPackdetail(spdEntity);
					Integer newId =detailService.getCount();
					SigSvsPackDtlRlnEntity rlnEntity=new SigSvsPackDtlRlnEntity();
					rlnEntity.setSigId(Integer.valueOf(id));
					rlnEntity.setDetailsId(newId);
					//插入中间表
					i=rlnService.insertSvsPackDtlInfo(rlnEntity);
			}
		}
		if( j>0 && p>0 && k>0){
			return JsonUtils.getJson(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
	/**
	 * 去新增页面
	 * Title ServicePack.java
	 * Description: 
	 * @param request
	 * @param model
	 * @return
	 * @author lzz
	 */
	@RequestMapping(value="/toAddDetailNew",method=RequestMethod.GET)
	public String toAddDetailNew(HttpServletRequest request,Model model){
		
		return "/servicepack/addDetailNew";
		
	}
	/**
	 * 去修改页面
	 * Title ServicePack.java
	 * Description: 
	 * @param request
	 * @param model
	 * @param id
	 * @return
	 * @author lzz
	 */
	@RequestMapping(value="/toUpdateDetailNew",method=RequestMethod.GET)
	public String toUpdateDetailNew(HttpServletRequest request,Model model,Integer id){
		
		ServicepackDetailEntity sDetailEntity=new ServicepackDetailEntity();
		sDetailEntity=detailService.getDetailInfo(id);
		model.addAttribute("sdEntity", sDetailEntity);
		return "/servicepack/updateDetailNew";
		
	}
	/**
	 * 新增服务项目
	 * Title ServicePack.java
	 * Description: 
	 * @param request
	 * @param model
	 * @param serviceName
	 * @param updateMan
	 * @param frequency
	 * @param serviceType
	 * @param serviceDetails
	 * @return
	 * @author lzz
	 */
	@ResponseBody
	@RequestMapping(value="/addDetailNew",method=RequestMethod.POST)
	public String addDetailNew(HttpServletRequest request,Model model,String serviceName,String updateMan,String frequency,String serviceType,String serviceDetails){
		
		 ServicepackDetailEntity spdEntity=new ServicepackDetailEntity();
		spdEntity.setServiceName(serviceName);
		//spdEntity.setServiceItem(serviceItem);
		spdEntity.setServiceDetails(serviceDetails);
		spdEntity.setFrequency(Integer.valueOf(frequency));
		//spdEntity.setOriginalPrice(Double.parseDouble((String)m1.get("originalPrice")));
		spdEntity.setServiceType(serviceType);
		spdEntity.setUpdateby(updateMan);
		spdEntity.setUpdatetime(new Date());
		spdEntity.setCreateby(updateMan);
		spdEntity.setCreatetime(new Date());
		spdEntity.setDelFlag(0);
		//新增项目
		int flag=detailService.addPackdetail(spdEntity);
			
		if( flag>0){
			return JsonUtils.getJson(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}

	/**
	 * 修改服务项目详情
	 * Title ServicePack.java
	 * Description: 
	 * @param request
	 * @param model
	 * @param id
	 * @param serviceName
	 * @param updateMan
	 * @param frequency
	 * @param serviceType
	 * @param serviceDetails
	 * @return
	 * @author lzz
	 */
	@ResponseBody
	@RequestMapping(value="/updateDetailNew",method=RequestMethod.POST)
	public String updateDetailNew(HttpServletRequest request,Model model,Integer id,String serviceName,String updateMan,String frequency,String serviceType,String serviceDetails){
		
		 ServicepackDetailEntity sdEntity=new ServicepackDetailEntity();
		 	sdEntity.setId(id);
			sdEntity.setServiceName(serviceName);
			sdEntity.setServiceDetails(serviceDetails);
			sdEntity.setFrequency(Integer.parseInt(frequency));
			sdEntity.setServiceType(serviceType);
			sdEntity.setUpdateby(updateMan);
			sdEntity.setUpdatetime(new Date());
			
			//修改方法
			int flag=detailService.updatePackDetail(sdEntity);
			
		if( flag>0){
			return JsonUtils.getJson(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}
	/**
	 * 逻辑删除服务项目
	 * Title ServicePack.java
	 * Description: 
	 * @param request
	 * @param model
	 * @param id
	 * @return
	 * @author lzz
	 */
	@ResponseBody
	@RequestMapping(value="/deleteDetailNew",method=RequestMethod.POST)
	public String deteleDetailNew(HttpServletRequest request,Model model,Integer id){
		String userName=(String) request.getSession().getAttribute("userName");
		ServicepackDetailEntity sDetailEntity = new ServicepackDetailEntity();
		sDetailEntity.setUpdateby(userName);
		sDetailEntity.setUpdatetime(new Date());
		sDetailEntity.setId(id);
		sDetailEntity.setDelFlag(1);
		Integer flag=detailService.deleteDetail(sDetailEntity);
		if( flag>0){
			return JsonUtils.getJson(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}
	
	@RequestMapping(value="/showRelation",method=RequestMethod.GET)
	public String showRelation(HttpServletRequest request,Model model){
		List<SigServicepackEntity> packList =packService.getAll();
		
		model.addAttribute("packList", packList);
		return "/servicepack/serviceRelation";
		
	}

	@RequestMapping(value="/addRelation",method=RequestMethod.GET)
	public String addRelation(HttpServletRequest request,Model model,String id){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("id", id);
		List<ServicepackDetailEntity> detailAll=detailService.getAll();
		model.addAttribute("id", id);
		model.addAttribute("detailAll", detailAll);
		model.addAttribute("detailAll2", JsonUtils.getJson(detailAll));
		return "/servicepack/addRelation";
	}
	
	/**
	 * 保存服务项目
	 * Title ServicePack.java
	 * Description: 
	 * @param request
	 * @param model
	 * @param id
	 * @return
	 * @author lzz
	 */
	@ResponseBody
	@RequestMapping(value="/saveRelation",method=RequestMethod.POST)
	public String saveRelation(HttpServletRequest request,Model model,Integer sigId,Integer detailId){
		SigSvsPackDtlRlnEntity rlnEntity=new SigSvsPackDtlRlnEntity();
		rlnEntity.setSigId(sigId);
		rlnEntity.setDetailsId(detailId);
		//插入中间表
		int flag=rlnService.insertSvsPackDtlInfo(rlnEntity);
		if( flag>0){
			return JsonUtils.getJson(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}
	/**
	 * 逻辑删除服务项目
	 * Title ServicePack.java
	 * Description: 
	 * @param request
	 * @param model
	 * @param id
	 * @return
	 * @author lzz
	 */
	@ResponseBody
	@RequestMapping(value="/deleteRelation",method=RequestMethod.POST)
	public String deleteRelation(HttpServletRequest request,Model model,Integer sigId,Integer detailId){
		
		SigSvsPackDtlRlnEntity rlnEntity=new SigSvsPackDtlRlnEntity();
		rlnEntity.setSigId(sigId);
		rlnEntity.setDetailsId(detailId);
		//删除方法,将关联表中的相关关系逻辑去除
		int flag=rlnService.updateRln(rlnEntity);
		if( flag>0){
			return JsonUtils.getJson(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}
}
